package com.xfcycle.zebrabikegp.customView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

/**
 * 水波纹样式搜索蓝牙组件
 * Created by xFcycle on 2017/5/4.
 */

public class RippleSearch extends View {

    private Paint paint;
    private int radius;

    private int screenWidth;
    private int screenHeight;
    private int radius_shade_value;


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            radius += radius_shade_value;
            paint.setStrokeWidth(radius);
            //改变透明度
            int alpha = paint.getAlpha();
            alpha -= 5;
            //设置当前透明度
            paint.setAlpha(alpha);
            //回调onDraw方法
            invalidate();
        }
    };

    public RippleSearch(Context context) {
        super(context);
        //初始化画笔
        setPaint();
        DisplayMetrics dm = getResources().getDisplayMetrics();
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;
        //算出半径渐变值
        radius_shade_value = screenWidth / 5 * 2 / 50;
    }

    public RippleSearch(Context context, AttributeSet attrs) {
        super(context, attrs);
        //初始化画笔
        setPaint();
        DisplayMetrics dm = getResources().getDisplayMetrics();
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;
        //算出半径渐变值
        radius_shade_value = screenWidth / 5 * 2 / 50;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (paint.getAlpha() > 0 ) {
            canvas.drawCircle(screenWidth / 2, screenHeight / 2, radius, paint);
            handler.sendEmptyMessageDelayed(0, 50);
        }
        if (paint.getAlpha() == 0 ) {
            radius = 50;
            paint.setAlpha(250);
            canvas.drawCircle(screenWidth / 2, screenHeight / 2, radius, paint);
            handler.sendEmptyMessageDelayed(0, 50);
        }

    }

    public void setPaint() {
        paint = new Paint();
        paint.setColor(Color.YELLOW);
        //定义抗锯齿
        paint.setAntiAlias(true);
        //设置样式
        paint.setStyle(Paint.Style.FILL);
        //设置默认的透明度
        paint.setAlpha(250);
        //设置绘制的线宽，即圆环的半径
        radius = 50;
        paint.setStrokeWidth(radius);

    }
}
